package com.hdc.hdc.warehouseManage.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hdc.hdc.warehouseManage.config.KVRedisConfig;
import com.hdc.hdc.warehouseManage.entity.Inorder;
import com.hdc.hdc.warehouseManage.service.UploadServiceI;
import com.hdc.hdc.warehouseManage.utill.AjaxReturnVO;
import com.hdc.hdc.warehouseManage.utill.FileUtil;
import com.hdc.hdc.warehouseManage.utill.OrderNumCresteUtil;

/**
 * 文件上传控制类
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月26日 上午9:29:22 
 *
 */
@RestController
@RequestMapping(value="/uploadControlle")
public class UploadFileController {
	
	@Value("${system_jkum.type}")
	private String systemType;
	
	@Autowired
	private UploadServiceI uploadServiceI;
	
	@RequestMapping(value="/getExcelModel")
	public void getExcelModel(HttpServletResponse response) {
		
		String fileName = "inorder_model.xls";  
		
		if(systemType != null && systemType.equals("window")) {
			FileUtil.downloadFile(response, fileName);  
		}else if(systemType != null){
			FileUtil.downloadFileInLinxu(response, fileName,systemType);  
		}
		
	}
	
	@RequestMapping(value="/uploadFileForInorder")
	public AjaxReturnVO uploadFileForInorder(@RequestParam("file")MultipartFile file,HttpServletRequest request) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		if(file.isEmpty()) {
			vo.setFail(vo, "不能上传空文件");
			return vo;
		}
		
		try {
			HttpSession session = WebUtils.toHttp(request).getSession();
	    	 
	    	String userinfoID = String.valueOf(session.getAttribute(KVRedisConfig.SESSION_ATTRIBUTE_USERID.getHaskey()));
	    	 
	    	String userinfoName = String.valueOf(session.getAttribute(KVRedisConfig.SESSION_ATTRIBUTE_USERNAME.getHaskey()));
			Date now = new Date();
	    	String orderNum = OrderNumCresteUtil.returnOrderNum("in");
	    	
	    	Inorder inorder = new Inorder();
	    	inorder.setInorderNum(orderNum);
	    	inorder.setCreator(userinfoID);
	    	inorder.setCreatorName(userinfoName);
	    	inorder.setCreateDate(now);
	    	inorder.setUpdateDate(now);
	    	inorder.setStatus(0);
	    	this.uploadServiceI.saveData(inorder,file);
	    	
		} catch (Exception e) {
			e.printStackTrace();
			vo.setFail(vo, e.getMessage());
		}
		
		return vo;
	}
	
	

}
